package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"out-manage-sys/cache"
	"out-manage-sys/model"
	"out-manage-sys/pkg"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")

	redis_key := "login" + username
	CreateData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if CreateData >= 3 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "错误三次，禁止登入",
		})
		return
	}

	var user model.User
	err := model.DB.Where("username = ?", username).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "查新失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "“账号或密码错误",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if CreateData == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 400,
				"msg":  "“密码错误三次，禁止两小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "“登入失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登入成功",
	})
	return
}
func RodeList(c *gin.Context) {
	rid := c.Query("rid")
	//SELECT
	//*
	//	FROM
	//
	//roles
	//
	//LEFT JOIN node_roles ON roles.id = node_roles.nid
	//WHERE node_roles.nid = 1

	var rode model.Role
	model.DB.Table("roles").
		Joins("LEFT JOIN node_roles ON roles.id = node_roles.rid").
		Where("node_roles.rid = ?", rid).Debug().Find(&rode)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": rode,
	})
	return

}
func Create(c *gin.Context) {
	boss_name := c.PostForm("boss_name")
	tel := c.PostForm("tel")

	boss := model.Boss{

		BossName: boss_name,
		Tel:      tel,
	}
	err := model.DB.Create(&boss).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func BossNameList(c *gin.Context) {
	bid := c.Query("bid")
	//SELECT
	//*
	//	FROM
	//
	//bosses
	//
	//LEFT JOIN boss_goods ON bosses.id = boss_goods.bid
	//WHERE boss_goods.bid =  1
	var boss_name model.Boss
	model.DB.Table("bosses").
		Joins("LEFT JOIN boss_goods ON bosses.id = boss_goods.bid").
		Where("boss_goods.bid = ?", bid).
		Debug().
		Find(&boss_name)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": boss_name,
	})
	return
}

func List(c *gin.Context) {

	var good []model.Good
	redis_key := "list"
	cachedata, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cachedata == "" {
		model.DB.Find(&good)
		jsonData, _ := json.Marshal(good)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cachedata), &good)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": good,
	})
	return
}

func Del(c *gin.Context) {
	id := c.PostForm("id")
	var del model.Good
	model.DB.Where("id = ?", id).Delete(&del)

	redis_key := "del_" + id
	cache.Rdb.Del(cache.Ctx, redis_key)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
	return
}
